package loc.dd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;

import loc.dd.entity.Permissions;
import loc.dd.service.WlbgcService;
import net.sf.json.JSON;
import net.sf.json.JSONObject;


/**
*@author 作者：huyaqi
*@version 创作时间:2023年8月28日下午4:46:16
*
*/
@RestController
@RequestMapping("/wlbgc")
public class WlbgcController {
private static Logger logger =LoggerFactory.getLogger(WlbgcController.class);
	
	@Autowired
	private WlbgcService wlbgcService;
	
	/**
	 * 查找全部
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "getjs" , method = RequestMethod.GET)
	public JSONArray getjs(HttpServletRequest request,HttpServletResponse response) {
		List<Permissions> gl= new ArrayList<Permissions>();
		String limit = request.getParameter("limit");
		String first = request.getParameter("first");
		HashMap<String,Integer> map = new HashMap<String, Integer>();
		JSONArray jo = new JSONArray();
		if(null != limit && !"".equals(limit)) {
			if(null != first && !"".equals(first)) {
				int a = Integer.parseInt(limit);
				int b = Integer.parseInt(first);
				map.put("first", b);
				map.put("limit", a);
				gl = wlbgcService.getjs(map);
			}
		}else {
			map.put("first", 0);
			map.put("limit", 10000);
			gl = wlbgcService.getjs(map);
		}
		jo.add(gl);
		System.out.println(gl);
		return jo;
    }

/**
 * 编辑新增角色
 * @param params
 * @param request
 * @param response
 * @return
 */	
	@CrossOrigin
	@RequestMapping(value = "addjs" , method = RequestMethod.POST)
	public Map<String,Object> addjs(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonx = (JSONObject) params.get("data"); 
		String rolename=(String)jsonx.get("rolename");
		Map<String, Object> map = new HashMap<>();
		int sl = wlbgcService.checkjs(rolename);
		if("" != jsonx.get("id") && !"".equals(jsonx.get("id"))){
			String rolename2 = wlbgcService.findjs((int) jsonx.get("id"));
			if(rolename2.equals(rolename)) {
				sl=0;
			}else {
			}
		}
		if(sl>0) {
			map.put("status", "407");
			return map;
		}else {
			net.sf.json.JSONArray auth2 = jsonx.getJSONArray("auth");
			String auth = auth2.toString().replaceAll("\"", "");
			auth=auth.replace("[", "").replace("]","");
			String note=(String)jsonx.get("note");
			Permissions pe= new Permissions();
			pe.setRolename(rolename);
			pe.setAuth(auth);
			pe.setNote(note);
			if("" != jsonx.get("id") && !"".equals(jsonx.get("id"))){
				int a = (int) jsonx.get("id");
				pe.setId(a);
				wlbgcService.editjs(pe);
			}else {
				wlbgcService.addjs(pe);
			}
			map.put("status", "200");
			return map;
		}
    }
	
	/**
	 * 删除角色
	 */
	@CrossOrigin
	@RequestMapping(value = "deljs")
	public JSONArray deljs(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONArray jo = new JSONArray();
		if(null != params.get("id") && !"".equals(params.get("id"))) {
			int id = (int) params.get("id");
			wlbgcService.deljs(id);
			jo.add("ok");
		}
		else {
			jo.add("not found");
		}
		return jo;
    }
	
	
	 @RequestMapping(value = "hello")
	    public String hello() {
	        return "hello";
	    }
	
}
